﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class UserMessageController : UserController
    {
        public string action = string.Empty;
        public int page=1;         //当前页码
        public int totalcount=0;   //OUT数据总数
        public IUser_messageService _user_message { get; set; }
        public IActionResult Index()
        {
            action = Request.Query["act"];
            if (string.IsNullOrEmpty(action))
                action = "accept";
            int.TryParse(Request.Query["page"], out page);
            page = page > 0 ? page : 1;
            ViewBag.action = action;
            ViewBag.Page = page;
            string strwhere="";
            switch (action)
            {
                case "system":
                    strwhere = "accept_user_name='" + userModel.user_name + "' and type=1";
                    break;
                case "accept":
                    strwhere = "accept_user_name='" + userModel.user_name + "' and type=2";
                    break;
                case "send":
                    strwhere = "post_user_name='" + userModel.user_name + "' and type=3";
                    break;
                case "add":
                    strwhere = "1=2";
                    break;
            }
            var list = _user_message.GetList(page, 50, "where "+strwhere, "post_time desc,id desc");
            ViewBag.messageList = list;
            totalcount = _user_message.RecordCount("where "+strwhere);
            string pageUrl = Utils.CombUrlTxt("/Web/Usermessage/Index", "page={0}&act={1}", "__id__",action);
            ViewBag.pagelist = Utils.OutPageList(50, page, totalcount, pageUrl, 8);
            return View();
        }
        public User_message model = new User_message();
        public IActionResult Show(int id)
        {
            if (!_user_message.Exists(id))
            {
                return ToError("出错了，您要浏览的页面不存在或已删除！");
            }
            model = _user_message.GetModel(id);
            if (model.accept_user_name != userModel.user_name && model.post_user_name != userModel.user_name)
            {
                return ToError("出错了，您所查看的并非自己的短消息！");
            }
            //设为已阅读状态
            _user_message.UpdateField(id, "is_read=1,read_time='" + DateTime.Now + "'");
            ViewBag.UserMessage = this;
            ViewBag.ID = id;
            return View(model);
        }
    }
}